﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using YAPL.Language.Ast;
using YAPL.Util;
namespace YAPL.Language.SourceParser.XML.NodeParsers {
	public class XmlParameterParser : XmlExpressionParser {
		public XmlParameterParser(XmlInputParser parser) : base(parser) {}
		public override Expression ParseNode(XElement node) {
			Require.True(node.Name.LocalName.Equals(XmlNames.ParameterExpression));
			var variableNode = node.Element(XmlNames.VariableExpression);
			var defaultValueNode = node.Element(XmlNames.ParameterDefault);
			Require.NotNull(variableNode);

			var variableExpr = (VariableExpression)Parser.ParseNode(variableNode);

			var defaultValueExpr = defaultValueNode != null ? Parser.ParseNode(defaultValueNode) : Expressions.Undefined();
			return Expressions.Parameter(variableExpr, defaultValueExpr);
		}
	}
}
